﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class adminstrator_planClassroom : System.Web.UI.Page
{
    public OleDbConnection myConnection;
    public static readonly string sconnectionstring = System.Configuration.ConfigurationManager.AppSettings["SQLConnString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection = new OleDbConnection(sconnectionstring);
        myConnection.Open();
        if (Session["UserName"] == null && Session["UserNo"] == null)
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
                "alert('登录超时（或未登录），请您重新登录！'); parent.location.href='../login.aspx'</script>", false);

        if (!IsPostBack)
        {
            string className = Request.QueryString["className"];
            Label1.Text = "(" + className + ")";
            bind();
        }
    }
    protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Gridview1.PageIndex = e.NewPageIndex;
        bind();
    }
    protected void Gridview1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string roomNo = Gridview1.DataKeys[e.NewEditIndex][0].ToString();
        string rTime = Gridview1.DataKeys[e.NewEditIndex][1].ToString();
        string aNo = Request.QueryString["applyNo"];
        string className = Request.QueryString["className"];
        string rPurpose = Request.QueryString["sjsyt"];
        string roomTime = Request.QueryString["roomTime"];
        string mystr1 = "update UsqbTb set rNo='" + roomNo + "' where aNo='" + aNo + "' ";
        OleDbCommand mycmd = new OleDbCommand(mystr1, myConnection);
        mycmd = new OleDbCommand(mystr1, myConnection);
        mycmd.ExecuteNonQuery();
        string mystr2 = "update RoomTB set rTime='"+ roomTime +"' +'" + rTime + "' ,sclass='" + className + "', sjsyt='" + rPurpose + "' where rNo='" + roomNo + "' ";
        mycmd = new OleDbCommand(mystr2, myConnection);
        mycmd.ExecuteNonQuery();
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('成功分配教室！');location.href='apply.aspx';</script>", false);
        string result = "申请成功";
        string mystr3 = "update UsqbTb set ApplyResult='" + result + "' where aNo='" + aNo + "' ";
        mycmd = new OleDbCommand(mystr3, myConnection);
        mycmd.ExecuteNonQuery();
        
    }
    public void bind()
    {   string roomTime = Request.QueryString["roomTime"];
        string mysql = "select * from RoomTB order by ID ";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataAdapter Adapter = new OleDbDataAdapter();
        Adapter.SelectCommand = mycmd;
        DataSet myDS = new DataSet();
        Adapter.Fill(myDS);
        if (myDS.Tables.Count > 0 && myDS.Tables[0].Rows.Count > 0)
        {
            Gridview1.DataSource = myDS;
            Gridview1.DataKeyNames = new string[] { "rNo", "rTime" };
            Gridview1.DataBind();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string aNo = Request.QueryString["applyNo"];
        string result = "申请未通过";
        string mystr4 = "update UsqbTb set ApplyResult='" + result + "', rNo='' where aNo='" + aNo + "' ";
        OleDbCommand mycmd = new OleDbCommand(mystr4, myConnection);
        mycmd = new OleDbCommand(mystr4, myConnection);
        mycmd.ExecuteNonQuery();
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('未分配教室！');location.href='apply.aspx';</script>", false);
    }
}